#include <android/log.h>
#include <jni.h>
#include "pkgnative_windows.h"


JavaVM* g_javaVm = nullptr;
JNIEnv* g_jniEnv = nullptr;
jobject g_cls = nullptr;


#define LOGTAG "PKGNATIVE_WINDOWS_JNI"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOGTAG, __VA_ARGS__)


jint JNI_OnLoad(JavaVM* vm, void* reserved) {
    g_javaVm = vm;

    if (vm->GetEnv((void**)&g_jniEnv, JNI_VERSION_1_6) != JNI_OK) {
        return JNI_ERR; 
    }

    jclass myClass = g_jniEnv->FindClass("com/cjmp/hao/pkgnative_windows/pkgnative_windows");
    g_cls = g_jniEnv->NewGlobalRef(myClass);

    return JNI_VERSION_1_6;
}


const char* FfiBar() {
    jclass cls = (jclass) g_cls;
    if (cls == nullptr) {
        return nullptr;
    }
    jmethodID ffiBar = g_jniEnv->GetStaticMethodID(cls, "Bar", "()Ljava/lang/String;");
    jobject result = g_jniEnv->CallStaticObjectMethod(cls, ffiBar);
    return g_jniEnv->GetStringUTFChars((jstring)result, nullptr);
}


const char* FfiBaz() {
    jclass cls = (jclass) g_cls;
    if (cls == nullptr) {
        return nullptr;
    }
    jmethodID ffiBar = g_jniEnv->GetStaticMethodID(cls, "Baz", "()Ljava/lang/String;");
    jobject result = g_jniEnv->CallStaticObjectMethod(cls, ffiBar);
    return g_jniEnv->GetStringUTFChars((jstring)result, nullptr);
}
